package com.bbm.communitysystem.service.impl;

import com.bbm.communitysystem.core.redis.PerRedis;
import com.bbm.communitysystem.domain.Per;
import com.bbm.communitysystem.domain.dto.role.PerDto;
import com.bbm.communitysystem.mapper.PerMapper;
import com.bbm.communitysystem.service.IPerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bbm
 * @since 2025-02-13
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PerServiceImpl extends ServiceImpl<PerMapper, Per> implements IPerService {


    private final PerRedis perRedis;

    @Override
    public int addPer(PerDto perDto) {

        Per per = new Per();

        per.setPerName(perDto.getName());
        per.setPerMod(perDto.getMod());
        per.setPerPath(perDto.getPath());
        per.setStatus(1);
        per.setCreateTime(System.currentTimeMillis() + "");
        per.setUpdateTime(per.getCreateTime());
        per.setUpdateBy(perDto.getUser());

        boolean save = save(per);

        if (!save) {
            return 1;
        }

        perRedis.setValue(per.getPerPath(), per);

        log.info("新增API：{}", per);

        return 0;
    }

    @Override
    public int putPer(PerDto perDto) {

        boolean update = lambdaUpdate()
                .set(!perDto.getName().isEmpty(), Per::getPerName, perDto.getName())
                .set(!perDto.getMod().isEmpty(), Per::getPerMod, perDto.getMod())
                .set(!perDto.getPath().isEmpty(), Per::getPerPath, perDto.getPath())
                .set(perDto.getStatus() != null, Per::getStatus, perDto.getStatus())

                .set(Per::getUpdateTime, System.currentTimeMillis() + "")
                .set(Per::getUpdateBy, perDto.getUser())
                .eq(Per::getId, perDto.getId())
                .update();

        if (!update) {
            return 1;
        }
        Per byId = getById(perDto.getId());
        perRedis.setValue(byId.getPerPath(), byId);
        log.info("修改API：{}", byId);
        return 0;
    }

    @Override
    public int delPer(Integer id) {

        Per byId = getById(id);

        boolean removeById = removeById(id);

        if (!removeById) {
            return 1;
        }
        perRedis.setValue(byId.getPerPath(), null);

        log.info("删除API：{}", byId);
        return 0;
    }

    @Override
    public PerDto getPer(Integer id) {

        Per byId = getById(id);

        if (byId == null) {
            return null;
        }

        PerDto perDto = new PerDto();

        perDto.setId(byId.getId());
        perDto.setName(byId.getPerName());
        perDto.setMod(byId.getPerMod());
        perDto.setPath(byId.getPerPath());
        perDto.setStatus(byId.getStatus());
        perDto.setUser(byId.getUpdateBy());

        return perDto;
    }

    @Override
    public PerDto[] getPerList(Integer[] ids) {
        PerDto[] perList = new PerDto[ids.length];

        for (int i = 0; i < ids.length; i++) {
            perList[i] = getPer(ids[i]);
        }

        return perList;
    }


}
